package top.xiaoqiannb.shapelessdao.procedures;

import top.xiaoqiannb.shapelessdao.network.ShapelessdaoModVariables;
import top.xiaoqiannb.shapelessdao.item.QimendunjiaItem;
import top.xiaoqiannb.shapelessdao.ShapelessdaoMod;

import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.advancements.AdvancementProgress;
import net.minecraft.advancements.Advancement;

public class ReadQimendunjiaProcedure {
	public static void execute(LevelAccessor world, Entity entity, ItemStack itemstack) {
		if (entity == null)
			return;
		if (itemstack.getItem() instanceof QimendunjiaItem)
			itemstack.getOrCreateTag().putString("geckoAnim", "classicbook.model.read");
		ShapelessdaoMod.queueServerWork(50, () -> {
			itemstack.shrink(1);
			if (((entity.getCapability(ShapelessdaoModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ShapelessdaoModVariables.PlayerVariables())).Faith).equals("Dao")) {
				{
					String _setval = "QimenI";
					entity.getCapability(ShapelessdaoModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
						capability.Sect = _setval;
						capability.syncPlayerVariables(entity);
					});
				}
				if (entity instanceof ServerPlayer _player) {
					Advancement _adv = _player.server.getAdvancements().getAdvancement(new ResourceLocation("shapelessdao:aqimendunjia"));
					AdvancementProgress _ap = _player.getAdvancements().getOrStartProgress(_adv);
					if (!_ap.isDone()) {
						for (String criteria : _ap.getRemainingCriteria())
							_player.getAdvancements().award(_adv, criteria);
					}
				}
			}
		});
	}
}
